package com.happyfamily.springboot.crowdsourcingplatform.service.weng.impl;

import com.happyfamily.springboot.crowdsourcingplatform.base.result.Results;
import com.happyfamily.springboot.crowdsourcingplatform.dao.weng.IndustryDynamicsDao;
import com.happyfamily.springboot.crowdsourcingplatform.model.IndustryDynamics;
import com.happyfamily.springboot.crowdsourcingplatform.service.weng.IndustryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class IndustryServiceImpl implements IndustryService
{
	@Autowired
	IndustryDynamicsDao industryDynamicsDao;
	@Autowired
private RedisTemplate<Object, Object> redisTemplate;

	@Override
	public Results<IndustryDynamics> getAllIndustryByPage(Integer startPosition, Integer limit)
	{
		return Results.success(industryDynamicsDao.countAllIndustryInfos().intValue(), industryDynamicsDao.getAllIndustryByPage(startPosition, limit));
	}

	@Override
	public Results<IndustryDynamics> getIndustryByFuzzy(String ntitle, String getDate, String ncontent, Integer startPosition, Integer limit)
	{
		return Results.success(industryDynamicsDao.getIndustryByFuzzy(ntitle, getDate, ncontent).intValue(), industryDynamicsDao.getIndustryByFuzzyPage(ntitle, getDate, ncontent, startPosition, limit));
	}

	@Override
	public int deleteIndustry(Integer nid)
	{
		//设置序列化
		redisTemplate.setKeySerializer(new StringRedisSerializer());

		int delete = industryDynamicsDao.deleteIndustry(nid);
		if (delete > 0)
		{
			doRefresh();
		}

		return delete;
	}

	@Override
	public int openIndustry(Integer nid)
	{
		//设置序列化
		redisTemplate.setKeySerializer(new StringRedisSerializer());

		int open = industryDynamicsDao.openIndustry(nid);
		if (open > 0)
		{
			doRefresh();
		}
		return open;
	}

	@Override
	public int closeIndustry(Integer nid)
	{
		//设置序列化
		redisTemplate.setKeySerializer(new StringRedisSerializer());

		int close = industryDynamicsDao.closeIndustry(nid);
		if (close > 0)
		{
			doRefresh();
		}
		return close;
	}


	@Override
	public int industryEdit(Integer nid, String ntitle)
	{
		//设置序列化
		redisTemplate.setKeySerializer(new StringRedisSerializer());

		int edit = industryDynamicsDao.industryEdit(nid, ntitle);
		if (edit > 0)
		{
			doRefresh();
		}
		return edit;
	}

	@Override
	public List<IndustryDynamics> frontGetIndustryByShu()
	{
		//设置序列化
		redisTemplate.setKeySerializer(new StringRedisSerializer());
		//取redis总数
		List<IndustryDynamics> industryDynamics = (List<IndustryDynamics>) redisTemplate.opsForValue().get("frontGetIndustryByShu");
		if (null == industryDynamics)
		{
			synchronized (this)
			{
				industryDynamics = (List<IndustryDynamics>) redisTemplate.opsForValue().get("frontGetIndustryByShu");
				if (null == industryDynamics)
				{
					industryDynamics = industryDynamicsDao.frontGetIndustryByShu();
					redisTemplate.opsForValue().set("frontGetIndustryByShu", industryDynamics);
				}
			}
		}
		return industryDynamics;
	}

	@Override
	public List<IndustryDynamics> frontGetIndustryBySheng()
	{

		//设置序列化
		redisTemplate.setKeySerializer(new StringRedisSerializer());
		//取redis总数
		List<IndustryDynamics> industryDynamics = (List<IndustryDynamics>) redisTemplate.opsForValue().get("frontGetIndustryBySheng");
		if (null == industryDynamics)
		{
			synchronized (this)
			{
				industryDynamics = (List<IndustryDynamics>) redisTemplate.opsForValue().get("frontGetIndustryBySheng");
				if (null == industryDynamics)
				{
					industryDynamics = industryDynamicsDao.frontGetIndustryBySheng();
					redisTemplate.opsForValue().set("frontGetIndustryBySheng", industryDynamics);
				}
			}
		}
		return industryDynamics;
	}

	@Override
	public List<IndustryDynamics> frontGetIndustryByZhi()
	{
		//设置序列化
		redisTemplate.setKeySerializer(new StringRedisSerializer());
		//取redis总数
		List<IndustryDynamics> industryDynamics = (List<IndustryDynamics>) redisTemplate.opsForValue().get("frontGetIndustryByZhi");
		if (null == industryDynamics)
		{
			synchronized (this)
			{
				industryDynamics = (List<IndustryDynamics>) redisTemplate.opsForValue().get("frontGetIndustryByZhi");
				if (null == industryDynamics)
				{
					industryDynamics = industryDynamicsDao.frontGetIndustryByZhi();
					redisTemplate.opsForValue().set("frontGetIndustryByZhi", industryDynamics);
				}
			}
		}

		return industryDynamics;
	}

	@Override
	public List<IndustryDynamics> frontGetIndustryByRu()
	{
		//设置序列化
		redisTemplate.setKeySerializer(new StringRedisSerializer());
		//取redis总数
		List<IndustryDynamics> industryDynamics = (List<IndustryDynamics>) redisTemplate.opsForValue().get("frontGetIndustryByRu");
		if (null == industryDynamics)
		{
			synchronized (this)
			{
				industryDynamics = (List<IndustryDynamics>) redisTemplate.opsForValue().get("frontGetIndustryByRu");
				if (null == industryDynamics)
				{
					industryDynamics = industryDynamicsDao.frontGetIndustryByRu();
					redisTemplate.opsForValue().set("frontGetIndustryByRu", industryDynamics);
				}
			}
		}
		return industryDynamics;
	}

	public void doRefresh()
	{
		//更新缓存
		List<IndustryDynamics> industryDynamicsShu = industryDynamicsDao.frontGetIndustryByShu();
		List<IndustryDynamics> industryDynamicsSheng = industryDynamicsDao.frontGetIndustryBySheng();
		List<IndustryDynamics> industryDynamicsZhi = industryDynamicsDao.frontGetIndustryByZhi();
		List<IndustryDynamics> industryDynamicsRu = industryDynamicsDao.frontGetIndustryByRu();
		redisTemplate.opsForValue().set("frontGetIndustryByShu", industryDynamicsShu);
		redisTemplate.opsForValue().set("frontGetIndustryBySheng", industryDynamicsSheng);
		redisTemplate.opsForValue().set("frontGetIndustryByZhi", industryDynamicsZhi);
		redisTemplate.opsForValue().set("frontGetIndustryByRu", industryDynamicsRu);
	}
}

